package ai;

import ai.service.InMemoryVectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@SpringBootApplication
public class HotelSpringAiApplication {
    public static void main(String[] args) {

        SpringApplication.run(HotelSpringAiApplication.class, args);

    }
    //向量数据库
    @Bean
    public InMemoryVectorStore vectorStore() {
        return new InMemoryVectorStore();
    }

    @Bean
    Runnable ingestRagToVectorStore(InMemoryVectorStore vectorStore,
                                    @Value("classpath:rag/hotel_cloud_sensitive.txt") ClassPathResource cloud,
                                    @Value("classpath:rag/hotel_cloud_po_sensitive.txt") ClassPathResource cloudPo,
                                    @Value("classpath:rag/term-of-service.txt") ClassPathResource tos) {

        return ()->{
            load(vectorStore, cloud);
            load(vectorStore, cloudPo);
            load(vectorStore, tos);
        };
    }

    private void load(InMemoryVectorStore store, ClassPathResource resource){
        if(resource==null || !resource.exists()) return;
        try(InputStream is = resource.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isr)){
            String content = br.lines().collect(Collectors.joining("\n"));
            store.add(content);
        }catch (IOException ignored){
        }
    }
}


